package com.example.utils;

import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtils {

    public static Map<String,String[]> dates = new HashMap<String, String[]>();

    public static void initDates(){
        dates.put("子时(23.0-0.59)",new String[]{"23:00:00","00:59:59"});
        dates.put("丑时(01.0-02.59)",new String[]{"01:00:00","02:59:59"});
        dates.put("寅时(03.0-04.59)",new String[]{"03:00:00","04:59:59"});
        dates.put("卯时(05.0-06.59)",new String[]{"05:00:00","06:59:59"});
        dates.put("辰时(07.0-08.59)",new String[]{"07:00:00","08:59:59"});
        dates.put("巳时(09.0-10.59)",new String[]{"09:00:00","10:59:59"});
        dates.put("午时(11.0-12.59)",new String[]{"11:00:00","12:59:59"});
        dates.put("未时(13.0-14.59)",new String[]{"13:00:00","14:59:59"});
        dates.put("申时(15.0-16.59)",new String[]{"15:00:00","16:59:59"});
        dates.put("酉时(17.0-18.59)",new String[]{"17:00:00","18:59:59"});
        dates.put("戌时(19.0-20.59)",new String[]{"19:00:00","20:59:59"});
        dates.put("亥时(21.0-22.59)",new String[]{"21:00:00","22:59:59"});
    }


    /**
     * 获取当天的零点时间
     *
     * @return 当天的零点时间戳
     */
    public static Date getTodayStartTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取昨天的开始时间
     *
     * @return 当天的零点时间戳
     */
    public static Date getYesterdayStartTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.MILLISECOND,0);
        calendar.set(Calendar.HOUR_OF_DAY,-24);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取昨天的结束时间
     *
     * @return 当天的零点时间戳
     */
    public static Date getYesterdayEndTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.MILLISECOND,0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        return calendar.getTime();
    }

    // 获取本周的开始时间
    public static Date getBeginDayOfWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return getDayStartTime(cal.getTime());
    }

    // 获取本周的结束时间
    public static Date getEndDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }

    // 获取本月的开始时间
    public static Date getBeginDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        return getDayStartTime(calendar.getTime());
    }

    // 获取本月的结束时间
    public static Date getEndDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth() - 1, day);
        return getDayEndTime(calendar.getTime());
    }

    // 获取本年的开始时间
    public static Date getBeginDayOfYear() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, getNowYear());
        cal.set(Calendar.MONTH, Calendar.JANUARY);
        cal.set(Calendar.DATE, 1);
        return getDayStartTime(cal.getTime());
    }

    // 获取本年的结束时间
    public static java.util.Date getEndDayOfYear() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, getNowYear());
        cal.set(Calendar.MONTH, Calendar.DECEMBER);
        cal.set(Calendar.DATE, 31);
        return getDayEndTime(cal.getTime());
    }

    // 获取某个日期的开始时间
    public static Date getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d)
            calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    // 获取某个日期的结束时间
    public static Date getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d)
            calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }
    // 获取今年是哪一年
    public static Integer getNowYear() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return Integer.valueOf(gc.get(1));
    }

    // 获取本月是哪一月
    public static int getNowMonth() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return gc.get(2) + 1;
    }

    public static String getYYYYMMDDTime(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        return simpleDateFormat.format(date);
    }

    public static String getYYYYMMDDHHMMSS(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        return simpleDateFormat.format(date);
    }

    public static String getYYYYMMDDTime(Date date){
        if(date == null){return "0000-00-00 00:00:00";}
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    /** type 1 今天 2 昨天 3 本周 4 本月 5 本年
     *
     * @param type
     * @return
     */
    public static Date[] getTypeDate(int type){
        Date start = DateUtils.getTodayStartTime();
        Date end = new Date();
        switch (type){
            case 1:
                start = DateUtils.getDayStartTime(end);
                end = DateUtils.getDayEndTime(end);
                break;
            case 2:
                start = DateUtils.getYesterdayStartTime();
                end = DateUtils.getYesterdayEndTime();
                break;
            case 3:
                start = DateUtils.getBeginDayOfWeek();
                end = DateUtils.getEndDayOfWeek();
                break;
            case 4:
                start = DateUtils.getBeginDayOfMonth();
                end = DateUtils.getEndDayOfWeek();
                break;
            case 5:
                start = DateUtils.getBeginDayOfYear();
                end = DateUtils.getEndDayOfYear();
                break;
        }
        return new Date[]{start,end};
    }

    public static int getRest(Date date){

        Date current = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(current);

        // 今天得开始
        Date dayStartTime = getDayStartTime(current);
        // 今天得结束
        Date dayEndTime = getDayEndTime(current);

        // 今天4点前 睡太晚
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
        calendar.get(Calendar.DAY_OF_MONTH), 3, 59, 59);

        if(date.getTime()>dayStartTime.getTime()&&date.getTime()<calendar.getTime().getTime()){
             return 1;
        }

        // 今天22点后 结束前  睡太晚
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), 21, 59, 59);
        if(date.getTime()>calendar.getTime().getTime()&&date.getTime()<dayEndTime.getTime()){
            return 1;
        }

        // 正常
        return 3;
    }
}
